﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class MenuDAO:IData<Menu> 
 	{
		public MenuDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Menu item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + Menu.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_Menu", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Menu.c_name, item.name), new SqlParameter("@" + Menu.c_title, item.title),
				 new SqlParameter("@" + Menu.c_url, item.url), new SqlParameter("@" + Menu.c_level1, item.level1),
				 new SqlParameter("@" + Menu.c_ordernumber, item.ordernumber), new SqlParameter("@" + Menu.c_isdelete, item.isdelete));
			if(t == 0)
				item.id = Convert.ToInt64(obj);
			return i;
		}

		public override Menu GetItem(IDataReader dr)
		{
			Menu obj = new Menu();
			obj.id = Convert.ToInt64(dr[Menu.c_id]);
			obj.name = Convert.ToString(dr[Menu.c_name]);
			obj.title = Convert.ToString(dr[Menu.c_title]);
			obj.url = Convert.ToString(dr[Menu.c_url]);
			obj.level1 = Convert.ToInt32(dr[Menu.c_level1]);
			obj.ordernumber = Convert.ToInt32(dr[Menu.c_ordernumber]);
			obj.isdelete = Convert.ToBoolean(dr[Menu.c_isdelete]);
			return obj;
		}

		public override List<Menu> GetAll()
		{
			return GetAll("sp_GetAllMenu", CommandType.StoredProcedure);
		}

		public override Menu GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override Menu GetItemById(Menu item){
			throw new NotImplementedException();
		}

	}

 }
